﻿using System.Collections.Generic;
using USNetFramework.Network;
using NetFramework;
using UnityEngine;
using Sproto;
using Common.Utils.Helper;

namespace Game.Logic.Handler
{
    public delegate void OnReceivedServer(SprotoObject _spObj);

    public class GameDataHandler : NetworkHandler
    {
        private Dictionary<string, OnReceivedServer> m_dictDataListener;

        public GameDataHandler()
        {
            m_dictDataListener = new Dictionary<string, OnReceivedServer>();
        }

        public void RegisterDataListener(string _protoName, OnReceivedServer _handler)
        {
            OnReceivedServer handler = null;
            if (!m_dictDataListener.TryGetValue(_protoName, out handler))
            {
                handler = _handler;
                m_dictDataListener.Add(_protoName, handler);
            }
            else
            {
                handler += _handler;
            }
        }

        public void UnregisterDataListener(string _protoName, OnReceivedServer _handler)
        {
            OnReceivedServer handler = null;
            if (!m_dictDataListener.TryGetValue(_protoName, out handler))
                return;

            if (handler == _handler)
            {
                m_dictDataListener.Remove(_protoName);
            }
            else
            {
                handler -= _handler;
            }
        }

        public override void OnSocketConnected(NetSocket _socket)
        {

        }

        public override void OnSocketConnectTimout(NetSocket _socket)
        {

        }

        // handler msg data
        public override bool OnHandleMessage(NetSocket _socket, NetPacket _packet)
        {
            //long recv = (long)TimeHelper.GetCurrentTimeStampMillisecond();

            SPNetPacket packet = (SPNetPacket)_packet;
            // TODO 错误码处理

            SprotoObject response = packet.Response;
            if (null != response)
            {
                string protoName = response.type.Substring(0, response.type.IndexOf("."));
                Debug.LogFormat("[SOCKET]we recv server response msg -> {0}", protoName);
                OnReceivedServer handler = null;
                if (m_dictDataListener.TryGetValue(protoName, out handler))
                {
                    handler.Invoke(response);
                }
            }

            SprotoObject request = packet.Request;
            if (null != request)
            {
                string protoName = request.type.Substring(0, request.type.IndexOf("."));
                Debug.LogFormat("[SOCKET]we recv server push msg -> {0}", protoName);
                OnReceivedServer handler = null;
                if (m_dictDataListener.TryGetValue(protoName, out handler))
                {
                    handler.Invoke(request);
                }
            }
            //long end = (long)TimeHelper.GetCurrentTimeStampMillisecond();
            //Debug.LogFormat("OnHandleMessage cost->{0}, recv->{1}, send->{2}", end-recv, recv, end);
            return true;
        }


        public override void OnSocketDisconnected(NetSocket _socket)
        {

        }
    }
}

